/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dartstools.interfaces;

import dartstools.ComponentMover;
import dartstools.Theme;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import javax.swing.JFormattedTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Chris
 */
public class MainGUI extends javax.swing.JFrame {

    private int targetScore = 301;
    private PlayerPanel[] playerPanels;

    /**
     * Creates new form MainGUI
     */
    public MainGUI() {
        initComponents();
        getContentPane().setBackground(Theme.BACKGROUND);
        new ComponentMover(this, this, topPanelInner);
        contentPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        String[] players = {"Chris", "Player 2"};
        playerPanels = new PlayerPanel[players.length];
        PlayerPanel p;
        for (int i = 0; i < players.length; i++) {
            p = new PlayerPanel(players[i], targetScore);
            playerPanels[i] = p;
            contentPanel.add(p);
        }
        addMouseWheelListener(new MouseAdapter() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (getFocusOwner() instanceof JFormattedTextField) {
                    JFormattedTextField selected = (JFormattedTextField) getFocusOwner();
                    int value = ((int) selected.getValue()) - e.getWheelRotation();
                    if (value >= 0 && value <= 60) {
                        selected.setValue(value);
                    }
                }
            }

        });
    }

    public void display() {
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void focusNextPlayerPanel(PlayerPanel current) {
        for (int i = 0; i < playerPanels.length; i++) {
            if (i == playerPanels.length - 1) {
                playerPanels[0].focusFirstScoreBox();
            }
            else if (playerPanels[i].equals(current)) {
                playerPanels[i + 1].focusFirstScoreBox();
                break;
            }
        }
    }

    public void focusPreviousPlayerPanel(PlayerPanel current) {
        for (int i = playerPanels.length - 1; i >= 0; i--) {
            if (i == 0) {
                playerPanels[playerPanels.length - 1].focusLastScoreBox();
            }
            else if (playerPanels[i].equals(current)) {
                playerPanels[i - 1].focusLastScoreBox();
                break;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        topPanelInner = new javax.swing.JPanel();
        exitButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        contentPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 51));
        setResizable(false);
        setUndecorated(true);

        topPanel.setBackground(new java.awt.Color(0, 0, 51));
        topPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 1, 0, 1));
        topPanel.setOpaque(false);

        topPanelInner.setBorder(Theme.BORDER);
        topPanelInner.setOpaque(false);

        exitButton.setBackground(new java.awt.Color(255, 0, 0));
        exitButton.setFont(new Font(Theme.FONT_FAMILY, Font.PLAIN, Theme.FONT_MEDIUM));
        exitButton.setForeground(Theme.FOREGROUND);
        exitButton.setText("x");
        //exitButton.setText("\u00D7");
        exitButton.setToolTipText("");
        exitButton.setBorder(null);
        exitButton.setBorderPainted(false);
        exitButton.setContentAreaFilled(false);
        exitButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        exitButton.setDefaultCapable(false);
        exitButton.setFocusPainted(false);
        exitButton.setFocusable(false);
        exitButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        exitButton.setMaximumSize(new java.awt.Dimension(30, 20));
        exitButton.setMinimumSize(new java.awt.Dimension(30, 20));
        exitButton.setPreferredSize(new java.awt.Dimension(30, 20));
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font(Theme.FONT_FAMILY, Font.PLAIN, Theme.FONT_MEDIUM));
        jLabel1.setForeground(Theme.FOREGROUND);
        jLabel1.setText("Darts Scoreboard");

        javax.swing.GroupLayout topPanelInnerLayout = new javax.swing.GroupLayout(topPanelInner);
        topPanelInner.setLayout(topPanelInnerLayout);
        topPanelInnerLayout.setHorizontalGroup(
            topPanelInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelInnerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        topPanelInnerLayout.setVerticalGroup(
            topPanelInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, topPanelInnerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(topPanelInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(topPanelInnerLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(9, 9, 9))
                    .addGroup(topPanelInnerLayout.createSequentialGroup()
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, topPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(topPanelInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(topPanelInner, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
        );

        contentPanel.setBackground(new java.awt.Color(0, 0, 51));
        contentPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 3, 6, 3));
        contentPanel.setOpaque(false);

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 422, Short.MAX_VALUE)
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 330, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(topPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                }
                catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                    System.err.println(e.getMessage());
                }
                MainGUI m = new MainGUI();
            }

        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel topPanel;
    private javax.swing.JPanel topPanelInner;
    // End of variables declaration//GEN-END:variables
}
